package com.article.service.ipl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.utils.StringUtils;
import com.article.mapper.ApArticleConfigMapper;
import com.article.mapper.ApArticleContentMapper;
import com.article.mapper.ApCollectionMapper;
import com.article.mapper.AparticleMappertu;
import com.article.service.ApArticleConfigService;
import com.article.service.ApArticleContentService;
import com.article.service.AparticleServicetu;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.usian.model.admin.pojos.AdUser;
import com.usian.model.atri.ApCollection;
import com.usian.model.atri.CollectionBehaviorDto;
import com.usian.model.atri.pojos.ApArticle;
import com.usian.model.atri.pojos.ApArticleConfig;
import com.usian.model.atri.pojos.ApArticleContent;
import com.usian.model.atri.pojos.dto.ArticleHomeDto;
import com.usian.model.atri.pojos.dto.ArticleInfoDto;
import com.usian.model.behavior.ApBehaviorEntry;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.dtos.LoginDto;
import com.usian.utils.common.AppJwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class AparticleServiceimpl extends ServiceImpl<AparticleMappertu, ApArticle> implements AparticleServicetu {
    private final Integer SIZE=50;
    @Resource
    private AparticleMappertu AparticleMappertu;

    @Resource
    ApArticleConfigMapper apArticleConfigMapper;

    @Resource
    ApArticleContentMapper apArticleContentMapper;

    @Resource
    ApCollectionMapper apCollectionMapper;


    @Override
    public ResponseResult load(ArticleHomeDto dto, Integer type) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        int min = Math.min(dto.getSize(), SIZE);
        dto.setSize(min);
        if (!type.equals(2) && !type.equals(1)){
            type=2;
        }
        if (dto.getMaxBehotTime()==null)dto.setMaxBehotTime(new Date());
        if (dto.getMinBehotTime()==null)dto.setMinBehotTime(new Date());

        return ResponseResult.okResult(AparticleMappertu.load(dto,type));
    }

    @Override
    public ResponseResult wenzhangxiangqing(ArticleInfoDto articleInfoDto) {
      if (articleInfoDto==null){
          return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
      }
      ApArticleConfig apArticleConfig = apArticleConfigMapper.selectapArticleConfig(articleInfoDto.getArticleId());

      if (apArticleConfig==null){
         return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"内容不存在");

      }

        System.out.println(apArticleConfig.getIsDelete());
        System.out.println(apArticleConfig.getIsComment());
        ApArticleContent ApArticleContent = apArticleContentMapper.selectApArticleContent(articleInfoDto.getArticleId());
        Map<String,Object > map=new HashMap<>();
        map.put("config",apArticleConfig);
        map.put("content",ApArticleContent);
        ResponseResult result = ResponseResult.okResult(map);
        return result;
    }

    @Override
    public ResponseResult shoucangxingwei(ApBehaviorEntry apBehaviorEntry, CollectionBehaviorDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<ApCollection> qw=new LambdaQueryWrapper<>();
        qw.eq(ApCollection::getEntryId,apBehaviorEntry.getId());
        ApCollection apCollection = apCollectionMapper.selectOne(qw);

        if (dto.getOperation()==0){
            if (apCollection==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            if (apCollection.getType()==1){
                LambdaUpdateWrapper<ApCollection> qw1=new LambdaUpdateWrapper<>();
                qw1.set(ApCollection::getType,0);
                apCollectionMapper.update(apCollection,qw1);
            }else {
                LambdaUpdateWrapper<ApCollection> qw1=new LambdaUpdateWrapper<>();
                qw1.set(ApCollection::getType,1);
                apCollectionMapper.update(apCollection,qw1);
            }

        }else {
            ApCollection apCollectionc=new ApCollection();
//            参数不对 需要查
//            apCollectionc.setArticleId();
            apCollectionc.setEntryId(apBehaviorEntry.getId());
            apCollectionc.setType(dto.getType());
            apCollectionc.setPublishedTime(dto.getPublishedTime());
            apCollectionc.setCollectionTime(new Date());
            apCollectionMapper.insert(apCollection);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
